package liuhao.base;

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class SessionUtil {
	
	/**
	 * 获得 session
	 * @return
	 */
	public static  SqlSession getSession(){
		
		
		try {
			//加载配置文件
			String resource = "mybatisConfig.xml";
			Reader  reader = Resources.getResourceAsReader(resource);
			
			//创建session工厂
			SqlSessionFactory  sessionFactory= new SqlSessionFactoryBuilder().build(reader);
			
			//ture 自动提交事务
			
			SqlSession session = sessionFactory.openSession(true);
			return session;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
